package zf.data.tools
{

	import flash.display.BitmapData;
	import flash.display.BlendMode;
	import flash.display.DisplayObject;
	import flash.display.Sprite;

	import flash.geom.ColorTransform;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	/**
	 * 位图法碰撞检测 
	 * @author mike.zhang
	 * @langversion 3.0 
     * @playerversion Flash 9 
	 */
	public class HitTestBitmap
	{
		//========复杂（精确）碰撞测试
		/**
		 * 复杂（精确）碰撞测试 
		 * @param target1 继承自DisplayObject的两个碰撞检测中的其一
		 * @param target2 继承自DisplayObject的两个碰撞检测中的其一
		 * @param accuracy 精确度
		 * @return 碰撞返回true;
		 * 
		 */
		public static function complexHitTestObject ( target1:DisplayObject, target2:DisplayObject,  accuracy:Number = 1 ):Boolean
		{
			return complexIntersectionRectangle(target1,target2,accuracy).width != 0;
		}
		//========返回两目标对象重叠的区域矩形
		/**
		 * 返回两目标对象重叠的区域矩形
		 * @param target1 继承自DisplayObject的两个碰撞检测中的其一
		 * @param target2 继承自DisplayObject的两个碰撞检测中的其一
		 * @return Rectangle 矩形
		 * 
		 */
		public static function intersectionRectangle ( target1:DisplayObject, target2:DisplayObject ):Rectangle
		{

			//如果目标对象不存在，或者简单碰撞测试为否，那么就不可能重叠
			if ( !target1.root || !target2.root || !target1.hitTestObject( target2 ) )
			{
				return new Rectangle  ;
			}

			//获取目标的边界信息Bounds
			var bounds1:Rectangle = target1.getBounds( target1.root );
			var bounds2:Rectangle = target2.getBounds( target2.root );

			// Determine test area boundaries.
			var intersection:Rectangle = new Rectangle();
			intersection.x   = Math.max( bounds1.x, bounds2.x );
			intersection.y    = Math.max( bounds1.y, bounds2.y );
			intersection.width      = Math.min( ( bounds1.x + bounds1.width ) - intersection.x, ( bounds2.x + bounds2.width ) - intersection.x );
			intersection.height = Math.min( ( bounds1.y + bounds1.height ) - intersection.y, ( bounds2.y + bounds2.height ) - intersection.y );

			return intersection;
		}
		//========返回复杂重叠的区域矩形
		/**
		 * 返回复杂重叠的区域矩形 
		 * @param target1 继承自DisplayObject的两个碰撞检测中的其一
		 * @param target2 继承自DisplayObject的两个碰撞检测中的其一
		 * @param accuracy 精确度
		 * @return Rectangle 矩形
		 * 
		 */
		public static function complexIntersectionRectangle ( target1:DisplayObject, target2:DisplayObject, accuracy:Number = 1 ):Rectangle
		{
			if ( accuracy <= 0 )
			{
				throw new Error("ArgumentError: Error #5001: Invalid value for accuracy",5001);
			}
			//如果简单碰撞为否，那么他们没有重叠
			if ( !target1.hitTestObject( target2 ) )
			{
				return new Rectangle  ;
			}

			var hitRectangle:Rectangle = intersectionRectangle( target1, target2 );

			if ( hitRectangle.width * accuracy <1 || hitRectangle.height * accuracy <1 )
			{
				return new Rectangle  ;
			}

			var bitmapData:BitmapData = new BitmapData( hitRectangle.width * accuracy, hitRectangle.height * accuracy, false, 0x000000 );

			//绘制第一个目标
			bitmapData.draw ( target1, getDrawMatrix( target1, hitRectangle, accuracy ), new ColorTransform( 1, 1, 1, 1, 255, -255, -255, 255 ) );
			//绘制第二个目标
			bitmapData.draw ( target2, getDrawMatrix( target2, hitRectangle, accuracy ), new ColorTransform( 1, 1, 1, 1, 255, 255, 255, 255 ), BlendMode.DIFFERENCE );


			//确定位图图像中完全包含指定颜色的所有像素的矩形区域,以找到重叠区域
			var intersection:Rectangle = bitmapData.getColorBoundsRect( 0xFFFFFFFF,0xFF00FFFF );

			bitmapData.dispose ();//释放bitmapData对象的内存，返回width,height为-1


			if ( accuracy != 1 )
			{
				intersection.x /= accuracy;
				intersection.y /= accuracy;
				intersection.width /= accuracy;
				intersection.height /= accuracy;
			}
			intersection.x += hitRectangle.x;
			intersection.y += hitRectangle.y;

			return intersection;
		}
		private static function getDrawMatrix ( target:DisplayObject, hitRectangle:Rectangle, accuracy:Number ):Matrix
		{
			//返回一个转换矩阵
			//accuracy精确度
			var localToGlobal:Point;

			var matrix:Matrix;

			var rootConcatenatedMatrix:Matrix = target.root.transform.concatenatedMatrix;

			localToGlobal = target.localToGlobal( new Point( ) );
			matrix = target.transform.concatenatedMatrix;
			matrix.tx = localToGlobal.x - hitRectangle.x;
			matrix.ty = localToGlobal.y - hitRectangle.y;

			matrix.a = matrix.a / rootConcatenatedMatrix.a;
			matrix.d = matrix.d / rootConcatenatedMatrix.d;
			if ( accuracy != 1 )
			{
				matrix.scale ( accuracy, accuracy );
			}

			return matrix;
		}
	}
}